﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using Newtonsoft.Json.Linq;
using System.Text;

namespace NetCoreCMS.Web.Areas.Admin.Controllers.Plugin
{
    public class Feedback_ReplyController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Plugin/feedback_reply.cshtml";
        private int id = 0;
        public IFeedbackService _feedback { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            ChkAdminLevel("plugin_feedback", CMSEnums.ActionEnum.View.ToString());
            ActionResult result = View(EDIT_RESULT_VIEW);
            int.TryParse(Request.Query["id"],out this.id);
            if (this.id == 0)
            {
                JscriptMsg("传输参数不正确！", "back");
                filterContext.Result = result;
                return;
            }
            else if (!_feedback.Exists(this.id))
            {
                JscriptMsg("信息不存在或已被删除！", "back");
                filterContext.Result = result;
                return;
            }
            ViewBag.Id = this.id.ToString();
        }
        //
        // GET: /admin/Reply/

        public ActionResult Index()
        {
            ShowInfo(this.id);
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitSave(string txtReContent)
        {
            ChkAdminLevel("plugin_feedback", CMSEnums.ActionEnum.Reply.ToString());
            var model = _feedback.GetModel(this.id);
            model.reply_content = Utils.ToHtml(txtReContent);
            model.reply_time = DateTime.Now;
            _feedback.Update(model);
            AddAdminLog(CMSEnums.ActionEnum.Reply.ToString(), "回复留言内容：" + model.title);
            ViewBag.ClientScript = JscriptMsg("留言回复成功！", "../feedback_list/index");
            return View(EDIT_RESULT_VIEW);
        }

        private void ShowInfo(int _id)
        {
            var model = _feedback.GetModel(_id);
            ViewData["model"] = model;
        }
    }
}
